<%@page import="java.math.BigDecimal"%>
<%@page import="com.hamizhong.iflowers.utils.MyColumnIndicator"%>
<%@page import="com.hamizhong.iflowers.web.vo.CartGoodsVO"%>
<%@page import="com.hamizhong.iflowers.web.vo.ProvinceVO"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%@include file="../common/common.jsp" %>
<title>初始化订单信息</title>
</head>

<%
	List<ProvinceVO> provinceVOList = (List<ProvinceVO>)request.getAttribute("provinceVOList");
	List<CartGoodsVO> cartGoodsVOList = (List<CartGoodsVO>)request.getAttribute("cartGoodsVOList");
	Double totalPrice = 0.0;
	for(int i=0;i<cartGoodsVOList.size();i++) {
		totalPrice += Double.valueOf(String.valueOf(cartGoodsVOList.get(i).getGoodsPrice())) * cartGoodsVOList.get(i).getGoodsCnt();
	}
	BigDecimal b = new BigDecimal(totalPrice);
	totalPrice = b.setScale(1,BigDecimal.ROUND_HALF_UP).doubleValue();
	String cartGoodsIdS = "";
	String cartGoodsCntS = "";
	for(int i=0;i<cartGoodsVOList.size();i++) {
		cartGoodsIdS += cartGoodsVOList.get(i).getGoodsId()+"-";
		cartGoodsCntS += cartGoodsVOList.get(i).getGoodsCnt()+"-";
	}
%>

<body>
	<input type="hidden" id="cartGoodsIdS" value="<%=cartGoodsIdS %>">
	<input type="hidden" id="cartGoodsCntS" value="<%=cartGoodsCntS %>">
	<%@include file="frame/top.jsp" %>
	<div class="main_div">
		<%@include file="frame/navigation.jsp" %>
		<div style="width: 990px;height: auto;margin: 0 auto;overflow: hidden;">
			<!--  收货人信息 -->
			<div style="width: 984px;height: auto;overflow: hidden;">
				<div style="width: 100%;height: 35px;text-align: left;font-size: 14px;line-height: 35px;">
					<span style="padding-left: 20px;font-weight: bold;">收货人信息</span>
				</div>
				<div style="width: 100%;height: 25px;line-height: 25px;font-size: 12px;">
					<div style="float: left;text-align: right;width: 120px;height: 25px;">
						<span style="color: red;font-weight: bold;">*</span>收货人姓名：
					</div>
					<input id="consigneeName" type="text" style="float: left;margin-top: 2px;width: 150px;"/>
				</div>
				<div style="width: 100%;height: 25px;line-height: 25px;font-size: 12px;text-align: left;">
					<div style="float: left;text-align: right;width: 120px;height: 25px;">
						<span style="color: red;font-weight: bold;">*</span>手机号码：
					</div>
					<input id="consigneePhone" type="text" style="float: left;margin-top: 2px;width: 150px;"/>
				</div>	
				<div style="width: 100%;height: 25px;line-height: 25px;font-size: 12px;text-align: left;">
					<div style="float: left;text-align: right;width: 120px;height: 25px;">
						<span style="color: red;font-weight: bold;">*</span>邮&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;编：
					</div>
					<input id="zip" type="text" style="float: left;margin-top: 2px;width: 150px;"/>
				</div>
				<div style="width: 100%;height: 25px;line-height: 25px;font-size: 12px;text-align: left;">
					<div style="float: left;text-align: right;width: 120px;height: 25px;">
						<span style="color: red;font-weight: bold;">*</span>省&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;市：
					</div>
					<select id="provinceSelect" style="width: 150px;" onchange="relatedToCitySelect()">
						<option id="province-0" value="0">*请选择*</option>
					<%
						for(int i=0;i<provinceVOList.size();i++) {
					%>
						<option id="province-<%=provinceVOList.get(i).getProvinceId() %>" value="<%=provinceVOList.get(i).getProvinceId() %>"><%=provinceVOList.get(i).getProvinceName() %></option>
					<%
						}
					%>
					</select>
					<select id="citySelect" style="width: 150px;">
						<option id="city-0">*请选择*</option>
						
					</select>
				</div>
				<div style="width: 100%;height: 25px;line-height: 25px;font-size: 12px;text-align: left;">
					<div style="float: left;text-align: right;width: 120px;height: 25px;">
						<span style="color: red;font-weight: bold;">*</span>地&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;址：
					</div>
					<input id="consigneeAddress" type="text" style="float: left;margin-top: 2px;width: 300px;"/>
				</div>
				<div style="width: 100%;height: 50px;line-height: 25px;font-size: 12px;text-align: left;">
					<div style="float: left;text-align: right;width: 120px;height: 25px;">
						备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注：
					</div>
					<textarea rows="" cols="" id="remarks" style="float: left;margin-top: 2px;width: 300px;height: 40px;"></textarea>
				</div>
			</div>
			
			<!-- 付款方式 -->
			<div style="clear: both;width: 100%;height: 150px;margin-top: 30px;border-top: 2px solid #B7D2DF;">
				<div style="width: 100%;height: 40px;line-height: 40px;text-align: left;">
					<span style="padding-left: 20px;font-weight: bold;font-size: 14px;">支付及配送方式</span>
				</div>
				<div style="width: 100%;height: 30px;line-height: 30px;font-size: 12px;text-align: left;">
					<span style="padding-left: 60px;">支付方式：货到付款</span>
				</div>
				<div style="width: 100%;height: 30px;line-height: 30px;font-size: 12px;text-align: left;">
					<span style="padding-left: 60px;">配送方式：快递运输</span>
				</div>
				<div style="width: 100%;height: 30px;line-height: 30px;font-size: 12px;text-align: left;">
					<span style="padding-left: 60px;">运　　费：0.00<span style="color: red;">（免运费）</span></span>
				</div>
			</div>
			
			<!-- 订单商品列表 -->
			<div style="clear: both;width: 100%;margin-top: 20px;border-top: 2px solid #B7D2DF;">
				<div style="width: 100%;height: 40px;line-height: 40px;text-align: left;">
					<span style="float: left;padding-left: 20px;font-weight: bold;font-size: 14px;">订单商品</span>
					<span style="float: right;font-size: 12px;margin-right: 20px;"><a style="color: #0070D7;" href="<%=request.getContextPath() %>/shoppingCart.action">去购物车修改</a></span>
				</div>
				<table width="950px" id="itemListTable" border="1px" style="margin-left: 20px;">
					<thead>
						<tr style="color: green;font-weight: bold;">
							<td width="60">商品图片</td>
							<td width="150">商品ID</td>
							<td width="300">商品名称</td>
							<td width="150">商品价格</td>
							<td width="150">库存状态</td>
							<td width="150">购买数量</td>
						</tr>
					</thead>
					<%
						for(int i=0;i<cartGoodsVOList.size();i++) {
							CartGoodsVO cartGoodsVO = cartGoodsVOList.get(i);
					%>
						<tr>
							<td><div style="width: 60px;height: 60px;"><a target="_blank" href="<%=request.getContextPath() %>/goodsItem.action?goodsId=<%=cartGoodsVO.getGoodsId() %>"><img width="56px" height="56px"  style="margin: 2px;" alt="" src="<%=request.getContextPath() %>/<%=cartGoodsVO.getGoodsPictureS() %>" /></a></div></td>
							<td><%=cartGoodsVO.getGoodsId() %></td>
							<td><a target="_blank" href="<%=request.getContextPath() %>/goodsItem.action?goodsId=<%=cartGoodsVO.getGoodsId() %>"><%=cartGoodsVO.getGoodsName() %></a></td>
							<td id="goodsPrice_<%=cartGoodsVO.getGoodsId() %>">￥<%=cartGoodsVO.getGoodsPrice() %></td>
							<td>
							<%
								if(cartGoodsVOList.get(i).getState() == MyColumnIndicator.G_ST_B) {
									out.print("现货");
								}
								if(cartGoodsVOList.get(i).getState() == MyColumnIndicator.G_ST_C) {
									out.print("已下架");
								}
							%>
							</td>
							<td><%=cartGoodsVO.getGoodsCnt() %></td>
						</tr>
					<%
						}
					%>
					<tr>
						<td colspan="6" style="text-align: right;"><font style="color: #FF6600;font-weight: bold;padding-right: 20px;">总计：￥<%=totalPrice %></font></td>
					</tr>
				</table>
			</div>
			
			<div style="width: 100%;height: auto;overflow: hidden;">
				<input onclick="submitOrders()" type="button" style="float: right;cursor: pointer;width: 150px;height: 35px;margin-right: 20px;" value="提交订单" >
			</div>
			
		</div>
	</div>
	<%@include file="frame/feet.jsp" %>
</body>

<script type="text/javascript">

	function submitOrders() {
		var provIdS = $('#provinceSelect').find('option:selected').attr('id');
		var provIdArr = provIdS.split('-');
		var selectProvinceId = provIdArr[1];
		var cityIdS = $('#citySelect').find('option:selected').attr('id');
		var cityIdArr = cityIdS.split('-');
		var selectCityId = cityIdArr[1];
		var consigneeName = $('#consigneeName').attr('value');
		var consigneePhone = $('#consigneePhone').attr('value');
		var consigneeAddress = $('#consigneeAddress').attr('value');
		var zip = $('#zip').attr('value');
		var remarks = $('#remarks').val();
		var cartGoodsIdS = $('#cartGoodsIdS').val();
		var cartGoodsCntS = $('#cartGoodsCntS').val();
		
		if(consigneeName == "") {
			alert("请填写收件人姓名！");
			$('#consigneeName').focus();
			return;
		}else if(consigneePhone == "") {
			alert("请填写收件人电话！");
			$('#consigneePhone').focus();
			return;
		}else if(zip == "") {
			alert("请如实填写收件地邮编！");
			$('#zip').focus();
			return;
		}else if(selectProvinceId == 0 || selectCityId == 0) {
			alert("请选择省份、城市！");
			$('#provinceSelect').focus();
			return;
		}else if(consigneeAddress == "") {
			alert("请如实填写收件地址！");
			$('#consigneeAddress').focus();
			return;
		}else {
			$.ajax({
				url:'<%=request.getContextPath() %>/ajaxAddOrders.action',
			data: {
				consigneeName:consigneeName,
				consigneePhone:consigneePhone,
				consigneeAddress:consigneeAddress,
				zip:zip,
				remarks:remarks,
				selectCityId:selectCityId,
				cartGoodsIdS:cartGoodsIdS,
				cartGoodsCntS:cartGoodsCntS
			},
			type:'post',
			async: false,
			dataType: 'json',
			success: function(res) {
				var rs = res.result;
				if(rs == 0) {
					//alert(res.msg);
					location.href = "<%=request.getContextPath() %>/addOrdersSuccessPage.action";
					}else {
						var msg = res.msg;
						alert(msg);
						return;
					}
				}
			});
		}
	}

	function relatedToCitySelect() {
		//var selectProvinceItem = $('#provinceSelect').val();
		//var provIdS = selectProvinceItem;
		//var provIdArr = provIdS.split('-');
		//var selectProvinceId = provIdArr[1];
		var selectProvinceId = $('#provinceSelect').val();
		if(selectProvinceId != 0) {
	 		$.ajax({
	 			url:'ajaxQueryCityListByProvinceId.action',
	 			data: {
	 				selectProvinceId:selectProvinceId
	 			},
				type:"post",
				async: false,
				dataType: 'json',
				success: function(res) {
					var rs = res.result;
					if(rs != 0) {
						alert(res.msg);
					}else {
						var data = res.data;
		 				var txt = "";
		 				for(var i=0;i<data.length;i++) {
		 					txt += '<option id="city-'+data[i].cityId+'">'+data[i].cityName+'</option>';
		 				}
		 				$('#citySelect').html(txt);
					}
	 				
	 			}
	 		});
		}else {
			$('#citySelect').html('<option id="city-0">*请选择*</option>');
		}
	}
</script>

</html>